<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		Config
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Config_model extends CI_Model {
	public function __construct() {
		parent::__construct();
	}

	public function show($cat, $deleted = 0, $q = '')
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->where('deleted', $deleted);
		$this->db->where('cat', $cat);
		if ($q != '')
		{
			$this->db->where(sprintf('(`name_vn` LIKE "%%%s%%" OR `keyword` LIKE "%%%s%%" OR `value` LIKE "%%%s%%")', $q, str_replace(' ', '_', $q), $q));
		}
		$last_login = $GLOBALS['user']['last_login'];
		$this->db->select('*, IF ('.sprintf('date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
		$this->db->order_by('name_vn asc');
		$query = $this->db->get('config');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function info($id)
	{
		$this->db->query("SET NAMES 'LATIN1'");
		$this->db->where('id', $id);
		$query = $this->db->get('config');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			$fields = $this->db->field_data('config');
			$default_data = array();
			foreach ($fields as $field)
			{
				$default_data[$field->name] = $field->default;
			}
			return $default_data;
		}
	}

	public function process($data, $id = '')
	{		
		if (!is_array($data))
		{
			return false;
		}
		$this->db->query('SET NAMES "LATIN1"');
		if ($id > 0)
		{
			unset($data['date_added']);
			$this->db->where('id', $id);
			$this->db->update('config', $data);
			write_log('update_config', $data['name_vn']);
		}
		else
		{
			$this->db->insert('config', $data);
			write_log('add_config', $data['name_vn']);
		}
		return true;
	}
}

/* End of file config_model.php */
/* Location: ./application/models/config_model.php */